﻿using Newtonsoft.Json;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YJ.JobSchedule;
using YJ.JobSchedule.Core;
using YJ.JobSchedule.Unit;
using AccessApi;
using System.Reflection;
using System.ServiceProcess;
using System.IO;
using YJ.JobSchedule.Configuration;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            /*
             这些测试需要将 相应的类的权限从internal 更改为public
             */
            #region dayUnit测试1
            //DayUnit unit = new DayUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 3,
            //        IntervalType = IntervalUnitType.day
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 30,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> { },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    StartTime = null,
            //    EndTime = null
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 8, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);

            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region dayUnit测试2
            //DayUnit unit = new DayUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 3,
            //        IntervalType = IntervalUnitType.day
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 30,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> { },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    StartTime = new DateTime(2017, 1, 20),
            //    EndTime = new DateTime(2017, 1, 25),
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 8, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region dayUnit测试3
            //DayUnit unit = new DayUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 3,
            //        IntervalType = IntervalUnitType.day
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 30,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.specific
            //    },
            //    StartTime = new DateTime(2017, 1, 20),
            //    //EndTime = new DateTime(2017, 1, 25),
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 8, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region weekUnit测试1
            //WeekUnit unit = new WeekUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.week
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 30,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    //StartTime = new DateTime(2017, 1, 20),
            //    //EndTime = new DateTime(2017, 1, 25),
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 8, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region weekUnit测试2
            //WeekUnit unit = new WeekUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.week
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    StartTime = new DateTime(2017, 2, 7),
            //    EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfWeek = new List<DayOfWeek> {
            //        DayOfWeek.Monday,
            //        DayOfWeek.Friday,
            //        DayOfWeek.Sunday
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 9, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region weekUnit测试2
            //WeekUnit unit = new WeekUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.week
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.specific
            //    },
            //    StartTime = new DateTime(2017, 2, 7),
            //    EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfWeek = new List<DayOfWeek> {
            //        DayOfWeek.Monday,
            //        DayOfWeek.Friday,
            //        DayOfWeek.Sunday
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 9, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region monthUnit测试1
            //MonthUnit unit = new MonthUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.month
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    //StartTime = new DateTime(2017, 2, 7),
            //    //EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfMonth = new List<int> {
            //      1,15,31
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 9, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region monthUnit测试2
            //MonthUnit unit = new MonthUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.month
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    StartTime = new DateTime(2017, 2, 7),
            //    EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfMonth = new List<int> {
            //      1,15,31
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 9, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region monthUnit测试3
            //MonthUnit unit = new MonthUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.month
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.specific
            //    },
            //    //StartTime = new DateTime(2017, 2, 7),
            //    //EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfMonth = new List<int> {
            //      1,15,31
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = new DateTime(2017, 1, 9, 3, 30, 30);
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region yearUnit测试1
            //YearUnit unit = new YearUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.year
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.recurring
            //    },
            //    StartTime = new DateTime(2017, 2, 7),
            //    EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfMonth = new List<int> {
            //      1,15,31
            //    },
            //    SpecificMonthOfYear = new List<int>
            //    {
            //        1,2,3,10,11,12
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = null;
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region yearUnit测试2
            //YearUnit unit = new YearUnit
            //{
            //    Interval = new IntervalUnit
            //    {

            //        Interval = 1,
            //        IntervalType = IntervalUnitType.year
            //    },
            //    UnitInDay = new ScheduleInDay
            //    {
            //        DayTimeRangeUnit = new DayTimeRange
            //        {
            //            StartTime = new TimeInDay(8),
            //            EndTime = new TimeInDay(18)
            //        },
            //        Interval = new IntervalUnit
            //        {
            //            Interval = 150,
            //            IntervalType = IntervalUnitType.minute
            //        },
            //        SpecificTimes = new List<TimeInDay> {
            //            new TimeInDay(9),
            //            new TimeInDay(11),
            //            new TimeInDay(13),
            //            new TimeInDay(18)
            //        },
            //        UnitType = UnitInDayType.specific
            //    },
            //    //StartTime = new DateTime(2017, 2, 7),
            //    //EndTime = new DateTime(2017, 4, 25),
            //    SpecificDayOfMonth = new List<int> {
            //      1,15,31
            //    },
            //    SpecificMonthOfYear = new List<int>
            //    {
            //        1,2,3,10,11,12
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = null;
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion

            #region SpecificTimes 测试
            //SpecificDateTimeUnit unit = new SpecificDateTimeUnit
            //{
            //    SpecificTimes = new List<DateTime>
            //    {
            //        new DateTime(2017,1,1),
            //        new DateTime(2017,1,15),
            //        new DateTime(2017,2,15),
            //        new DateTime(2017,3,15),
            //        new DateTime(2017,7,15),
            //        new DateTime(2017,1,7),
            //        new DateTime(2017,2,3)
            //    }
            //};

            //int count = 1;
            //DateTime? lastTime = null;
            //while (true)
            //{
            //    lastTime = unit.GetNextRunTime(lastTime);
            //    if (lastTime == null)
            //        break;
            //    Console.WriteLine(count + ":" + lastTime);

            //    count++;
            //    if (count == 51)
            //        break;
            //}

            #endregion


            Job job = new Job("a", "测试", () => { Console.WriteLine(DateTime.Now); });
            job = job.EveryDays(1).RecurringInDay(3, IntervalUnitType.second);

            //DateTime? last = null;
            //int count = 0;
            //while (true)
            //{
            //    last = job.Unit.GetNextRunTime(last);
            //    Console.WriteLine(last);

            //    if (count++ == 50)
            //        break;
            //}
            JobManager.AddAndStartJob(job);
            Console.ReadKey();
        }
    }

}
